package com.seekting.autosetup;

import android.util.Log;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import androidx.test.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;

public class AutonSetup {
    private final Map<String, Skipper> mMap = new HashMap<>();
    private static final String PKG_PROVISION = "com.android.provision";
    private static final String PKG_SETTINGS = "com.android.settings";


    private UiDevice mDevice;


    @Test
    public void autoSetup() {
        //获取设备用例
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        Utils.wakeup(mDevice);
        mMap.put("com.android.provision/.activities.DefaultActivity", new SimpleDepthClickSkipper("com.android.provision:id/next", 4));
        mMap.put("com.android.provision/.activities.LanguagePickerActivity", new SimpleDepthClickSkipper("com.android.provision:id/next_global", 7));
        mMap.put("com.android.provision/.activities.LocalePickerActivity", new RegionSkipper());
        mMap.put("com.android.settings/.Settings$WifiSettingsActivity", new WifiSkipper());
        mMap.put("com.android.provision/.activities.TermsActivity", new AggreeSkipper());
        mMap.put("com.android.provision/.activities.SimCardDetectionActivity", new SimpleClickSkipper("com.android.provision:id/next_global"));
        mMap.put("com.google.android.setupwizard/.network.CaptivePortal", new IgnoreSkiper());
        mMap.put("com.google.android.setupwizard/.network.NetworkTimeoutActivity", new TextSkipper("Skip"));
        mMap.put("com.google.android.apps.restore/com.google.android.apps.pixelmigrate.component.FlowChoiceActivity", new TextSkipper("Don’t copy"));
        mMap.put("com.google.android.gms/.auth.uiflows.addaccount.PreAddAccountActivity",
                new IgnoreSkiper());

        mMap.put("com.google.android.gms/.auth.uiflows.addaccount.ErrorActivity",
                new ErrorSkiper());
        mMap.put("com.google.android.setupwizard/.network.NetworkActivity",
                new IgnoreSkiper());

        while (true) {
            Utils.wakeup(mDevice);
            String topActivity = Utils.getTopActivity(mDevice);

            if (topActivity == null) {
                Utils.sleep();
                continue;
            }
            Log.d("seekting", "AutonSetup.autoSetup() topActivity=" + topActivity);
            if (mMap.containsKey(topActivity)) {
                mMap.get(topActivity).skip(mDevice);

                Utils.waitForIdle(mDevice);
            } else {
                Utils.sleep();
                if ("com.miui.home/.launcher.Launcher".equals(topActivity)) {


                    clientId();
                    break;
                }

            }

        }
        Log.d("seekting", "AutonSetup.autoSetup() over");


    }

    private void clientId() {
//        echo -n 'ro.com.google.clientidbase='
//        adb shell getprop ro.com.google.clientidbase
//        echo -n 'ro.com.google.clientidbase.ms='
//        adb shell getprop ro.com.google.clientidbase.ms
//        echo -n 'ro.com.google.clientidbase.vs='
//        adb shell getprop ro.com.google.clientidbase.vs
//        echo -n 'ro.com.google.clientidbase.tx='
//        adb shell getprop ro.com.google.clientidbase.tx
//        echo -n 'ro.com.google.clientidbase.cr='
//        adb shell getprop ro.com.google.clientidbase.cr
//        echo -n 'ro.com.google.rlz_ap_whitelist='
//        adb shell getprop ro.com.google.rlz_ap_whitelist
//        echo -n 'ro.com.google.rlzbrandcode='
//        adb shell getprop ro.com.google.rlzbrandcode
//        echo -n 'PREMIER_TIER='
//        adb shell pm list features|grep com.google.android.feature.PREMIER_TIER
//        echo  'search_client_id:'
//        adb shell content query --uri content://com.google.settings/partner|grep search_client_id
        dumpProp("ro.com.google.clientidbase");
        dumpProp("ro.com.google.clientidbase.ms");
        dumpProp("ro.com.google.clientidbase.vs");
        dumpProp("ro.com.google.clientidbase.tx");
        dumpProp("ro.com.google.clientidbase.cr");
        dumpProp("ro.com.google.rlz_ap_whitelist");
        dumpProp("ro.com.google.rlzbrandcode");

    }

    private void dumpProp(String prop) {
        String str = Utils.executeShellCommand(mDevice, "getprop " + prop);
        Log.d("seekting", prop + "=" + str);
    }

    interface Skipper {
        void skip(UiDevice uiDevice);
    }

    static class SimpleDepthClickSkipper implements Skipper {
        String id;
        int depth;

        public SimpleDepthClickSkipper(String id, int depth) {
            this.id = id;
            this.depth = depth;
        }


        @Override
        public void skip(UiDevice uiDevice) {
            UiObject2 uiObject2 = Utils.findById(uiDevice, id, depth);
            Utils.click(uiObject2);

        }

    }

    static class SimpleClickSkipper implements Skipper {
        String id;

        public SimpleClickSkipper(String id) {
            this.id = id;

        }

        @Override
        public void skip(UiDevice uiDevice) {
            Utils.clickById(uiDevice, id);

        }

    }

    static class RegionSkipper implements Skipper {

        @Override
        public void skip(UiDevice uiDevice) {
            Utils.setTextById(uiDevice, "com.android.provision:id/search_input", "India");
            Utils.waitForWindowUpdate(uiDevice, PKG_PROVISION);
            UiObject2 radioBox = Utils.findById(uiDevice, "com.android.provision:id/item_view", 9);
            if (radioBox != null && !radioBox.isChecked()) {
                Utils.parentClick(radioBox);
            }
            UiObject2 next = Utils.findById(uiDevice, "com.android.provision:id/next_global");
            Utils.click(next);
        }
    }

    static class WifiSkipper implements Skipper {

        String wifiName = "NETGEAR16";
        String userName = "";
        String pwd = "bydjnhy298";

        @Override
        public void skip(UiDevice uiDevice) {

//            BySelector selector = By.hasChild(By.res("android:id/title").text(wifiName));
//            UiObject2 uiObject2 = uiDevice.wait(Until.findObject(selector), Utils.TIMEOUT);
//            Utils.click(uiObject2);

            UiObject2 uiObject2 = Utils.findByIdAndText(uiDevice, "android:id/title", wifiName);
            Utils.clickRect(uiDevice, uiObject2);

            Utils.waitForIdle(uiDevice);
            if (Utils.setTextById(uiDevice, "com.android.settings:id/password", pwd)) {
                Utils.waitForIdle(uiDevice);
            }
            if (Utils.clickById(uiDevice, "android:id/button1")) {
                Utils.waitForIdle(uiDevice);
            }
            //resource-id="com.android.settings:id/provision_btn_extra" skip
            //resource-id="com.android.settings:id/provision_arrow_next"
            Utils.clickById(uiDevice, "com.android.settings:id/provision_arrow_next");

        }
    }

    static class AggreeSkipper implements Skipper {
        @Override
        public void skip(UiDevice uiDevice) {
            UiObject2 uiObject = Utils.findById(uiDevice, "com.android.provision:id/checkbox_agree");
            if (uiObject != null && !uiObject.isChecked()) {
                uiObject.click();
            }


            Utils.clickById(uiDevice, "com.android.provision:id/next_global");
        }
    }

    static class IgnoreSkiper implements Skipper {

        @Override
        public void skip(UiDevice uiDevice) {

            Utils.sleep();
        }
    }

    static class ErrorSkiper implements Skipper {

        @Override
        public void skip(UiDevice uiDevice) {
//            adb shell settings put secure user_setup_complete 1
//            adb shell settings put global device_provisioned 1
            String setup_complete = "settings put secure user_setup_complete 1";
            String device_provisioned = "settings put global device_provisioned 1";
            String str = Utils.executeShellCommand(uiDevice, setup_complete);
            Log.d("seekting", "ErrorSkiper.setup_complete()" + str);
            str = Utils.executeShellCommand(uiDevice, device_provisioned);
            Log.d("seekting", "ErrorSkiper.device_provisioned()" + str);
            Utils.sleep();
        }
    }

    static class TextSkipper implements Skipper {
        public TextSkipper(String text) {
            this.text = text;
        }

        String text;

        @Override
        public void skip(UiDevice uiDevice) {
            UiObject2 uiObject = Utils.findByIdAndText(uiDevice, "", text);
            Utils.click(uiObject);
        }
    }

}
